<template>
  <el-dialog :visible.sync="dialogVisible" :title="title" width="500px" @close="onClose">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" label-position="left">
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="form.nickName" disabled size="medium"/>
      </el-form-item>

      <el-form-item label="角色" prop="roles">
        <treeselect v-model="form.roleIds" :multiple="true" :options="options" :normalizer="normalizer"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { getOne, getUserRole, dispense } from '@/api/sysUser'
import { getList } from '@/api/sysRole'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: { Treeselect },
  data() {
    return {
      dialogVisible: false,
      title: '添加',
      form: {
        id: '',
        nickName: '',
        roleIds: []
      },
      rules: {
        nickName: [
          {
            required: true,
            message: '请输入昵称',
            trigger: 'blur'
          }
        ],
        roles: [
          {
            required: true,
            message: '请选择角色信息',
            trigger: 'blur'
          }
        ]
      },
      options: []
    }
  },
  methods: {
    init() {
      if (this.form.id) {
        getOne(this.form.id).then((res) => {
          if (res && res.code === 20000) {
            const rtn = res.data
            this.form.id = rtn.id
            this.form.nickName = rtn.nickName
          }
        })
        getUserRole(this.form.id).then(res => {
          if (res.code === 20000) {
            for (var i = 0; i < res.data.length; i++) {
              this.form.roleIds.push(res.data[i].id)
            }
          }
        })
      }
    },
    onShow(id) {
      if (id) {
        this.form.id = id
        this.title = '编辑'
        this.init()
      } else {
        this.title = '添加'
      }
      this.dialogVisible = true
    },
    onClose() {
      this.$refs['ruleForm'].resetFields()
      this.form = this.$options.data().form
      this.dialogVisible = false
    },
    onSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          console.log(this.form)
          dispense(this.form)
            .then((res) => {
              if (res && res.code === 20000) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.dialogVisible = false
              }
            })
            .catch((e) => {
            })
        } else {
          return false
        }
      })
    },
    normalizer(node) {
      return {
        id: node.id,
        label: node.name
      }
    }
  },
  mounted() {
    getList({}).then(res => {
      if (res.code === 20000) {
        this.options = res.data
      }
    })
  }
}
</script>

<style scoped>
.el-form {
  padding-left: 60px;
  padding-right: 60px;
}

/deep/ .el-form-item__content {
  line-height: inherit !important;
}

</style>
